<?php


namespace app\common\model;

use app\model\AdminAttendanceRecord;
use app\model\AdminAttendanceSys;
use app\model\AdminRechargeRecord;
use app\model\AdminSchool;
use app\model\AdminStaff;
use think\facade\Log;

class RechargeRecordModel extends AdminRechargeRecord
{
    // 后台考勤记录
    public static function getList(Array $data) {
        $size = $data['size'] ?? config('apiadmin.ADMIN_LIST_DEFAULT');
        $start = $data['page'] ?? 1;
        $school_name = $data['school_name'] ?? '';
        $name = $data['name'] ?? '';
        $phone = $data['phone'] ?? '';
        $order_no = $data['order_no'] ?? '';
        $recharge_time = $data['recharge_time'] ?? '';

        $obj = (new AdminRechargeRecord());

        $where = [];
        if ($school_name) {
            $school_id_arr = AdminSchool::where('is_delete', 0)
                ->where('name','like', "%{$school_name}%")
                ->column('id');
            array_push($where, ['school_id', 'in', $school_id_arr]);
        }
        if ($name) {
            array_push($where, ['name', 'like', "%{$name}%"]);
        }
        if ($phone) {
            array_push($where, ['phone', 'like', "%{$phone}%"]);
        }
        if ($order_no) {
            $obj = $obj->where('order_no', 'like', "%{$order_no}%");
        }
        if (!empty($recharge_time) && !is_array($recharge_time)) {
            $recharge_time = explode( ",", $recharge_time);
        }
        if (!empty($recharge_time) && array_filter($recharge_time)) {
            // 自动选择到结束时间
            $recharge_time[1] = $recharge_time[1].' 23:59:59';
            $obj = $obj->whereTime('recharge_time', 'between', $recharge_time);
        }
        $model = $obj->hasWhere('staff', $where)->with(['staff','school'])
            ->append(['order_type_text'])
            ->order(['create_time' => 'DESC']);
        if (!isset($data['export'])) {
            $list = $model->paginate(['page' => $start, 'list_rows' => $size])->toArray();
            $listInfo = $list['data'];
            foreach ($listInfo as $key => &$value) {
                if ($value['recharge_time'])
                    $value['recharge_time'] = date('Y-m-d H:i:s', $value['recharge_time']);
                $value['money'] = $value['money'] / 100;
            }
            return [
                'data' => $listInfo,
                'total' => $list['total']
            ];
        } else {
            $list = $model->select()->toArray();
            foreach ($list as $key => &$value) {
                if ($value['recharge_time'])
                    $value['recharge_time'] = date('Y-m-d H:i:s', $value['recharge_time']);
                $value['money'] = $value['money'] / 100;
            }
            return [
                'data' => $list
            ];
        }
    }
}